package com.chat.datea.acitivity;

import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.chat.datea.R;
import com.chat.datea.fragment.SongAlbumFragment;
import com.chat.datea.fragment.SongFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周橹 on 15/10/22.
 */
public class SingerIntroActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView singer_area, singer_name;
    private ImageView singer_img, singer_back;
    private ImageLoader imageLoader;
    private TabLayout tabLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_singer_introduce);

        setupToolbar();
        setupViewPager();
        setupCollapsingToolbar();

        singer_area = (TextView) findViewById(R.id.singer_area);
        singer_name = (TextView) findViewById(R.id.singer_name);
        singer_img = (ImageView) findViewById(R.id.singer_img);
        singer_back = (ImageView) findViewById(R.id.singer_intro_back);
        tabLayout = (TabLayout) findViewById(R.id.tabLayout);

        singer_back.setOnClickListener(this);
    }

    private void setupCollapsingToolbar() {
        final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapse_toolbar);

        collapsingToolbarLayout.setTitleEnabled(false);
    }

    private void setupViewPager() {
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        setupViewPager(viewPager);

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tabLayout);
        tabLayout.setupWithViewPager(viewPager);
    }

    private void setupToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("Singer name");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFrag(new SongFragment(), "歌曲");
        adapter.addFrag(new SongAlbumFragment(), "专辑");

        viewPager.setAdapter(adapter);

    }

    static class ViewPagerAdapter extends FragmentPagerAdapter {

        private final List<Fragment> mFragments = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        public void addFrag(Fragment fragment, String title) {
            mFragments.add(fragment);
            mFragmentTitleList.add(title);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mFragmentTitleList.get(position);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.singer_intro_back:
                finish();
                break;
            default:
                break;
        }
    }


}
